<template>
	<u-cell class="leftconbox" stop center>
		<view class="leftcon" slot="value">
			<view class="leftcheckbox" @click.stop="checkboxChange">
				<icon :color="ischeck?'green':'grey'" type="success" size="20" />
				<view class="allse">
					全选
				</view>
			</view>
			<view class="heji">
				合计:<text class="countprice">￥{{totlepay}}</text>
			</view>
			<view class="count" @click="count">
				结算
			</view>
		</view>
	</u-cell>
</template>

<script>
	import {
		mapGetters,
		mapState
	} from 'vuex'
	export default {
		props: {
			addrstr: {
				type: String,
				required: true
			}
		},
		data() {
			return {
				ischeck: true,
			}
		},
		methods: {
			checkboxChange() {
				this.ischeck = !this.ischeck
				this.$store.commit('seAllCheckbox', this.ischeck)
				// this.$set(this.shopItem,'goods_state',!this.shopItem.goods_state)
				// this.$store.commit('seCheckbox',this.shopItem)
			},
			async count() {
				// 开发期间，注释掉真实的订单价格，
				// order_price: this.checkedGoodsAmount,
				// 写死订单总价为 1 分钱，
				//收货地址
				//购物车列表
				console.log(this.mycart)
				const orderInfo = {
					order_price: 0.01,consignee_addr: this.addrstr,
					goods: this.mycart.filter(x => x.goods_state).map(x => ({
						goods_id: x.goods_id,
						goods_number: x.goods_count,
						goods_price: x.goods_price
					}))
				}
				// 获取订单id
				let {data:orderRes}=await uni.$http.post('/api/public/v1/my/orders/create',orderInfo) 
				if (orderRes.meta.status !== 200) return uni.$showMsg('创建订单失败！')
				let orderId=orderRes.message.order_number
				// 获取预支付参数
				let {data:prePayRes}=await uni.$http.post('/api/public/v1/my/orders/req_unifiedorder',{order_number:orderId})
				console.log(prePayRes,'prePayRes')
				if (prePayRes.meta.status !== 200) return uni.$showMsg('预付订单生成失败！')
				let payParam=prePayRes.message.pay
				// 发起支付
				const [err, succ] = await uni.requestPayment(payParam)
				if(err) return uni.$showMsg('订单未支付！')
				const payRes = await uni.$http.post('/api/public/v1/my/orders/chkOrder', { order_number: orderNumber })
				if (payRes.meta.status !== 200) return uni.$showMsg('订单未支付！')
				console.log(payRes)
				uni.showToast({
				     title: '支付完成！',
				     icon: 'success'
				})
			},
		},
		computed: {
			...mapGetters(['totlepay']),
			...mapState(['mycart'])
		}
	}
</script>

<style>
	uni-page-wrapper {
		position: relative;
	}

	.leftcon {
		width: 100%;
		padding: 0 28rpx;
		padding-right: 0;
		position: fixed;
		bottom: 0;
		left: 0;
		box-sizing: border-box;
		display: flex;
		justify-content: space-between;
		align-items: center;
		height: 100rpx;
		background-color: white;
		z-index: 999;
	}

	.allse {
		padding-left: 10rpx;
	}

	.leftcheckbox {
		display: flex;
	}

	.countprice {
		color: rgb(195, 21, 20)
	}

	.count {
		width: 230rpx;
		height: 100%;
		color: white;
		text-align: center;
		line-height: 100rpx;
		background-color: rgb(216, 30, 60);
	}
</style>
